<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/9
 * Time: 18:42
 */

namespace App\HttpController\V1;

use App\Common\Checking;
use wechatpay\WxApp;
use alipay\AppPay;
use App\HttpController\Taoran\Functions\Publics;
class Vip extends Auth
{

    protected $values;
    protected $appid='wx08e600eb7ad66cd1';
    protected $mchid='1527074921';
    protected $key='tv0ov06vgvavh1ml5enx9dgocetsvbyg';//自己设置的微信商家key

    /**
     * 获取会员价格
     * @return bool
     */
    function vip_price(){
        $vip_price=$this->db->where('name','vip_price')->getValue('config','value');
        $balance=$this->db->where('id',$this->user->id)->getValue('users','balance');
        return $this->writeJson( 200,['vip_price'=>$vip_price,'balance'=>$balance], '成功' );
    }

    /**
     * 充值会员
     * @return bool
     */
    function payVip(){
        $param=$this->input('num,pay_style,pay_password');
        $Publics=new Publics();
        $userinfo=$this->db
            ->where('id',$this->user->id)
            ->getOne('users','pay_password');
        $vip_price=$this->db->where('name','vip_price')->getOne('config','value');
        $out_trade_no=Checking::ordeSnByUnique('order_num',$this->db,'vip_order');
        $money=$vip_price['value']*$param['num'];
        $money=sprintf("%.2f",$money);
//        $money=0.01;
        if($param['pay_style']==1){//支付宝
            $notify_url=API_URL.'/v1/notify/ali_vip';//异步地址
            $AliPay=new AppPay();
            $data=$AliPay->app_pay('明心堂-会员充值',$out_trade_no,$money,$notify_url);
            $this->db->insert('vip_order',['user_id'=>$this->user->id,'num'=>$param['num'],'pay_style'=>$param['pay_style'],'order_num'=>$out_trade_no,'money'=>$money]);
            return $this->writeJson(200,$data,'成功');
        }elseif ($param['pay_style']==2){
            $notify_url=API_URL.'/v1/notify/wx_vip';//异步地址
            $WxApp=new WxApp();
            $data=$WxApp->getPrePayOrder('明心堂-充值会员',$out_trade_no,$money,$notify_url,$this->request()->getHeaders()['real-ip'][0]);
            $this->db->insert('vip_order',['user_id'=>$this->user->id,'num'=>$param['num'],'pay_style'=>$param['pay_style'],'order_num'=>$out_trade_no,'money'=>$money]);
            return $this->writeJson(200,$data,'成功');
        }elseif ($param['pay_style']==3){
            if(!Checking::checkPassword($userinfo['pay_password'],$param['pay_password'])){
                return $this->writeJson(-203,null,'交易密码错误');
            }

            $this->db->startTransaction();
            try{
                $balance=$this->db->where('id',$this->user->id)->getValue('users','balance');

                if($money>$balance){
                    return $this->writeJson( -203, null, '余额不足');
                }
                #生成订单
                $this->db->insert('vip_order',['user_id'=>$this->user->id,'num'=>$param['num'],'pay_style'=>$param['pay_style'],'order_num'=>$out_trade_no,'money'=>$money,'status'=>1]);
                $this->db->where('id',$this->user->id)->setDec('users','balance',$money);//减少余额
                //记录入库
                $this->db->insert('users_account_log',[
                    'user_id'=>$this->user->id,
                    'type'=>5,
                    'class'=>2,
                    'coin_type'=>'study',
                    'money'=>$money,
                    'remark'=>'续费会员',
                    'createtime'=>date('YmdHis'),
                    'before_money'=>$balance
                ]);
                $user_info=$this->db->where('id',$this->user->id)->getOne('users','level,vip_expire_time');
                $num=$param['num']*12;
                if($user_info['level']==1){
                    $vip_expire_time=date("Y-m-d H:i:s", strtotime("+{$num} month"));
                }else{
                    $vip_expire_time=date("Y-m-d H:i:s", strtotime("+{$num} month",strtotime($user_info['vip_expire_time'])));
                }
                $this->db->where('id',$this->user->id)->update('users',['level'=>2,'vip_expire_time'=>$vip_expire_time]);
                $insert=$Publics->call_inspiration($this->user->id,$money,1,$this->db);
                $group=$Publics->group($this->user->id,$money,1,$this->db);
                if(!$insert||!$group){
                    $this->db->rollback();
                    return $this->writeJson( -204, null, '失败' );
                }
                $this->db->commit();
                return $this->writeJson( 200, null, '续费成功' );
            }catch (\Exception $e){
                $this->db->rollback();
                return $this->writeJson( -201, null, $e->getMessage());
            }
        }else{
            return $this->writeJson( -202, null, '支付方式错误');
        }

    }



}